package com.zang.string;

/**
 * @author ZVerify
 * @since 2022/10/15 11:08
 * @see <a href="https://leetcode.cn/problems/reverse-string-ii/">...</a>
 **/
public class 翻转字符串II {

    public String reverseStr(String s, int k) {

        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i+=2*k) {
            if (i+k>=s.length()){
                reverse(chars,i,s.length()-1);
                continue;
            }
            reverse(chars,i,i+k-1);
        }

        return new String(chars);

    }

    public void reverse(char[] s,int left, int right) {

        while (left < right){
            s[left]^=s[right];
            s[right]^=s[left];
            s[left]^=s[right];
            left++;
            right--;
        }
    }
}
